home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / radix32.c < prev    next >
C/C++ Source or Header  |  1991-11-27  |  1KB  |  67 lines

  1. /*  $Revision: 1.3 $
  2. **
  3. **  Radix-32 strings divide a number into five-bit nibbles and use the
  4. **  alphabet 0..9a..v to represent 0..32.
  5. */
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <time.h>
  9. #include "configdata.h"
  10. #include "clibrary.h"
  11.  
  12.  
  13. static char    ALPHABET[] =
  14.     "0123456789abcdefghijklmnopqrstuv";
  15.  
  16.  
  17. /*
  18. **  Turn a number into a Radix-32 string.  Assume the number fits into
  19. **  32 bits.
  20. */
  21. void
  22. Radix32(l, buff)
  23.     register unsigned long    l;
  24.     register char        *buff;
  25. {
  26.     register char        *p;
  27.     register int        i;
  28.     char            temp[10];
  29.  
  30.     /* Simple sanity checks. */
  31.     if ((l &= 0xFFFFFFFFL) == 0) {
  32.     *buff++ = ALPHABET[0];
  33.     *buff = '\0';
  34.     return;
  35.     }
  36.  
  37.     /* Format the string, in reverse. */
  38.     for (p = temp; l; l >>= 5)
  39.     *p++ = ALPHABET[(int)(l & 037)];
  40.  
  41.     /* Reverse it. */
  42.     for (i = p - temp; --i >= 0; )
  43.     *buff++ = *--p;
  44.     *buff = '\0';
  45. }
  46.  
  47.  
  48. #if    0
  49. /*
  50. **  Return a Radix-32 string as a number, or ~0 on error.
  51. */
  52. unsigned long
  53. Decode32(p)
  54.     register char        *p;
  55. {
  56.     register unsigned long    l;
  57.     register char        *cp;
  58.  
  59.     for (l = 0; *p; p++) {
  60.     if ((cp = strchr(ALPHABET, *p)) == NULL)
  61.         return ~0;
  62.     l = (l << 6) + cp - ALPHABET;
  63.     }
  64.     return l;
  65. }
  66. #endif    /* 0 */
  67.